package com.luanye.enjoy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luanye.enjoy.dto.UserFollowDTO;
import com.luanye.enjoy.entity.Follow;
import com.luanye.enjoy.mapper.FollowMapper;
import com.luanye.enjoy.service.FollowService;
import com.luanye.enjoy.service.UserService;
import com.luanye.enjoy.utils.BeanCopyUtils;
import com.luanye.enjoy.utils.SecurityUtils;
import com.luanye.enjoy.vo.RespBean;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.luanye.enjoy.constants.RedisConst.FOLLOW_KEY;

@Service("followService")
@AllArgsConstructor
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    private final StringRedisTemplate stringRedisTemplate;

    private final UserService userService;

    @Override
    public RespBean isFollow(Long followUserid) {
        Long userId = SecurityUtils.getUserId();
        Long count = query().eq("user_id", userId).eq("follow_user_id", followUserid).count();
        return RespBean.success(count > 0);
    }

    @Override
    public RespBean follow(Long followUserid, Boolean isFollow) {
        Long userId = SecurityUtils.getUserId();
        String key = FOLLOW_KEY + userId;
        if (isFollow)
        {
            //关注
            Follow follow = new Follow()
                    .setFollowUserId(followUserid).setUserId(userId)
                    ;
            boolean isSuccess = save(follow);
            if (isSuccess)
            {
                stringRedisTemplate.opsForSet().add(key, followUserid.toString());
            }
        }
        else
        {
            //取关
            boolean isSuccess = remove(new QueryWrapper<Follow>().eq("user_id", userId).eq("follow_user_id", followUserid));
            if (isSuccess)
            {
                stringRedisTemplate.opsForSet().remove(key, followUserid.toString());
            }
        }
        return RespBean.success();
    }

    @Override
    public RespBean followCommons(Long id) {
        Long userId = SecurityUtils.getUserId();
        String currentKey = FOLLOW_KEY + userId;
        String targetKey = FOLLOW_KEY + id;

        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(currentKey, targetKey);
        if (intersect == null || intersect.isEmpty()){
            return RespBean.success(Collections.emptyList());
        }

        //解析id
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());

        List<UserFollowDTO> users = userService.listByIds(ids).stream()
                .map(user -> BeanCopyUtils.copyBean(user, UserFollowDTO.class))
                .collect(Collectors.toList());

        return RespBean.success(users);
    }
}
